﻿using System;
using DBServer.Selenium.Extenium.Exception;
using DBServer.Selenium.Extenium.IFixture;
using DBServer.Selenium.Extenium.Path;
using Selenium;

namespace DBServer.Selenium.Extenium.Fixtures
{
    public class CheckBoxFixture : ExtComponentFixture, IFixture.ICheckBox
    {

        public CheckBoxFixture(ISelenium driver, IPath path)
            : base(driver, path)
        {
        }

        public ICheckBox SetValue(bool value)
        {
            SafeExec(Path.Eval() + ".setValue(" + value.ToString().ToLower() + ");");
            return this;
        }

        public ICheckBox RequireValue(bool value)
        {
            var result = bool.Parse(SafeExec(Path.Eval() + ".checked;"));
            if ( result != value )
            {
                throw new ExteniumException("CheckBox expected value: " + value + ", found: " + result);
            }
            return this;
        }

        public ICheckBox RequireEnabled()
        {
            var result = bool.Parse(SafeExec(Path.Eval() + ".disabled"));
            if (result)
            {
                throw new ExteniumException("CheckBox is not enabled");
            }
            return this;
        }

        public ICheckBox RequireDisabled()
        {
            var result = bool.Parse(SafeExec(Path.Eval() + ".disabled"));
            if (!result)
            {
                throw new ExteniumException("CheckBox is enabled");
            }
            return this;
        }

        public ICheckBox Check()
        {
            RequireUncheck();
            SetValue(true);
            RequireCheck();
            return this;
        }

        public ICheckBox UnCheck()
        {
            RequireUncheck();
            SetValue(true);
            RequireCheck();
            return this;
        }

        public ICheckBox RequireCheck()
        {
            var result = bool.Parse(SafeExec(Path.Eval() + ".checked"));
            if (!result)
            {
                throw new ExteniumException("CheckBox is not cheked");
            }
            return this;
        }

        public ICheckBox RequireUncheck()
        {
            var result = bool.Parse(SafeExec(Path.Eval() + ".checked"));
            if (result)
            {
                throw new ExteniumException("CheckBox is cheked");
            }
            return this;
        }
    }
}